local Lua__lijiyi = fk.CreateSkill{
  name = "Lua__lijiyi",
  anim_type = "control",
}

Fk:loadTranslationTable{
  ["Lua__lijiyi"] = "里技",
  [":Lua__lijiyi"] = "每轮限X+1次，你可以于出牌阶段或受到伤害时你选择一项直至本轮结束：1：攻击范围无限且造成伤害时改为火焰伤害；2：对距离为一的角色使用牌无次数限制且不可被响应；3：使用牌指定目标时可以取消之并获得目标的一张;4：对距离一以外的角色使用牌时额外结算一次；5：造成伤害后目标角色使用的下一张牌无效。你第一次选择某项时，回复1点体力并摸三张牌。若你已受伤、本轮场上有角色死亡、装备区没有牌、手牌数不为全场最高，X均+1。一名角色死亡时，X+1。",
  ["@lijiyi_used"] = "里技已使用",
  ["@@lijiyi_effect1-round"] = "里技攻击范围",
  ["@@lijiyi_effect2-round"] = "里技不可响应",
  ["@@lijiyi_effect3-round"] = "里技目标拿牌",
  ["@@lijiyi_effect4-round"] = "里技额外结算",
  ["@@lijiyi_effect5-round"] = "里技伤害无效",
  ["lijiyi_X"] = "里技X值",
  ["lijiyi_round-round"] = "本轮已用次数",
  ["#lijiyi_choose"] = "里技：请选择一项效果",
  ["lijiyi1"] = "攻击范围无限且伤害为火焰",
  ["lijiyi2"] = "对距离1角色使用牌无次数且不可响应",
  ["lijiyi3"] = "使用牌时可取消目标并获得其一张牌", 
  ["lijiyi4"] = "对距离1+角色使用牌额外结算",
  ["lijiyi5"] = "造成伤害后目标下一张牌无效",
  ["#lijiyi_cancel"] = "里技：你可以取消一名目标并获得其一张牌",
  ["#lijiyi_disable"] = "里技：因你的技能，%src 使用的牌无效",
  ["@@lijiyi_death_round-round"] = "本轮有角色死亡",
  ["@@lijiyi_disable-round"] = "下一张牌无效",
}

local function getXValue(player)
  local x = 0
  -- 若已受伤，X+1
  if player.hp < player.maxHp then
    x = x + 1
  end
  
  -- 本轮场上有角色死亡，X+1
  if player:getMark("@@lijiyi_death_round-round") > 0 then
    x = x + 1
  end
  
  -- 装备区没有牌，X+1
  if #player:getCardIds(Player.Equip) == 0 then
    x = x + 1
  end
  
  -- 手牌数不为全场最高，X+1
  if table.every(Fk:currentRoom().alive_players, function(p)
    return player:getHandcardNum() <= p:getHandcardNum() or player.hp <= p.hp
  end) then
    x = x + 1
  end
  -- 死亡角色数增加X
  x = x + player:getMark("lijiyi_X")
  
  return x
end

-- 记录死亡角色
Lua__lijiyi:addEffect(fk.Death, {
    is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(Lua__lijiyi.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "lijiyi_X", 1)
    room:addPlayerMark(player, "@@lijiyi_death_round-round", 1)
  end,
})

-- 出牌阶段发动
Lua__lijiyi:addEffect("active", {
  can_use = function(self, player)
    local x = getXValue(player)
    return player.phase == Player.Play and player:usedSkillTimes(Lua__lijiyi.name,Player.HistoryRound) < x + 1
  end,
  card_filter = Util.FalseFunc,
  interaction = function(self, player)
    local choices = {}
    if player:getMark("@@lijiyi_effect1-round") == 0 then
        table.insert(choices,"lijiyi1")
    end
    if player:getMark("@@lijiyi_effect2-round") == 0 then
        table.insert(choices,"lijiyi2")
    end
    if player:getMark("@@lijiyi_effect3-round") == 0 then
        table.insert(choices,"lijiyi3")
    end
    if player:getMark("@@lijiyi_effect4-round") == 0 then
        table.insert(choices,"lijiyi4")
    end
    if player:getMark("@@lijiyi_effect5-round") == 0 then
        table.insert(choices,"lijiyi5")
    end
    return UI.ComboBox {choices = choices}
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    
    local index = tonumber(string.sub(self.interaction.data, 7, 7))
    
    -- 设置效果标记
    room:addPlayerMark(player, "@@lijiyi_effect" .. index .. "-round", 1)
    room:addPlayerMark(player, "lijiyi_round-round", 1)
    
    -- 第一次选择某项，回复1点体力并摸三张牌
    if player:getMark("lijiyi_first" .. index) == 0 then
      room:addPlayerMark(player, "lijiyi_first" .. index, 1)
      room:recover({
        who = player,
        num = 1,
        recoverBy = player,
        skillName = Lua__lijiyi.name
      })
      player:drawCards(3, Lua__lijiyi.name)
    end
  end,
})

-- 受到伤害时发动
Lua__lijiyi:addEffect(fk.DamageInflicted, {
    
  can_trigger = function(self, event, target, player, data)
    if target ~= player or not player:hasSkill(Lua__lijiyi.name) then return false end
    
    local x = getXValue(player)
    local used_times = player:getMark("lijiyi_round-round")
    return player:usedSkillTimes(Lua__lijiyi.name,Player.HistoryRound)  < x + 1
  end,
  
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local options = {"Cancel"}
    if player:getMark("@@lijiyi_effect1-round") == 0 then
        table.insert(options,"lijiyi1")
    end
    if player:getMark("@@lijiyi_effect2-round") == 0 then
        table.insert(options,"lijiyi2")
    end
    if player:getMark("@@lijiyi_effect3-round") == 0 then
        table.insert(options,"lijiyi3")
    end
    if player:getMark("@@lijiyi_effect4-round") == 0 then
        table.insert(options,"lijiyi4")
    end
    if player:getMark("@@lijiyi_effect5-round") == 0 then
        table.insert(options,"lijiyi5")
    end
    local choice = room:askToChoice(player, {
      choices = options,
      skill_name = Lua__lijiyi.name,
      prompt = "#lijiyi_choose",
    })
    
    if choice ~= "Cancel" then
      event:setCostData(self, {choice = choice})
      return true
    end
    return false
  end,
  
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choice = event:getCostData(self).choice
    local index = tonumber(string.sub(choice, 7, 7))
    
    -- 设置效果标记
    room:addPlayerMark(player, "@@lijiyi_effect" .. index .. "-round", 1)
    room:addPlayerMark(player, "lijiyi_round-round", 1)
    
    -- 第一次选择某项，回复1点体力并摸三张牌
    if player:getMark("lijiyi_first" .. index) == 0 then
      room:addPlayerMark(player, "lijiyi_first" .. index, 1)
      room:recover({
        who = player,
        num = 1,
        recoverBy = player,
        skillName = Lua__lijiyi.name
      })
      player:drawCards(3, Lua__lijiyi.name)
    end
    
    return false
  end,
})

-- 效果1：攻击范围无限且造成伤害时改为火焰伤害
Lua__lijiyi:addEffect("atkrange", {
    correct_func = function (self, from, to)
      if from:getMark("@@lijiyi_effect1-round") > 0 then
        return 999
      end
    end,
  })

Lua__lijiyi:addEffect(fk.DamageCaused, {
    is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(Lua__lijiyi.name) and player:getMark("@@lijiyi_effect1-round") > 0
  end,
  on_use = function(self, event, target, player, data)
    data.damageType = fk.FireDamage
  end,
})

-- 效果2：对距离为一的角色使用牌无次数限制且不可被响应
Lua__lijiyi:addEffect(fk.TargetSpecifying, {
    is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(Lua__lijiyi.name) and 
           player:getMark("@@lijiyi_effect2-round") > 0 and data.to and player:distanceTo(data.to) == 1
  end,
  on_use = function(self, event, target, player, data)
    player:addCardUseHistory(data.card.trueName, -1)
    data.disresponsive = true
  end,
})

-- 效果3：使用牌指定目标时可以取消之并获得目标的一张
Lua__lijiyi:addEffect(fk.TargetSpecified, {
    is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(Lua__lijiyi.name) and 
           player:getMark("@@lijiyi_effect3-round") > 0 and not data.to:isNude() and data.to ~= player
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local to = data.to
    
    local choice = room:askForChoice(player, {"yes", "no"}, Lua__lijiyi.name, "#lijiyi_cancel")
    if choice == "yes" then
      event:setCostData(self, {target = to})
      return true
    end
    return false
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).target
    
    data:cancelTarget(data.to)
    
    -- 获得目标的一张牌
    local id = room:askForCardChosen(player, to, "he", Lua__lijiyi.name)
    if id then
      room:obtainCard(player, id, false, fk.ReasonPrey)
    end
    
    return false
  end,
})

-- 效果4：对距离一以外的角色使用牌时额外结算一次
Lua__lijiyi:addEffect(fk.CardUsing, {
    is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if target ~= player or not player:hasSkill(Lua__lijiyi.name) or 
       player:getMark("@@lijiyi_effect4-round") == 0 then
      return false
    end
    
    -- 检查是否有目标距离大于1
    for _, p in ipairs(data.tos) do
      if player:distanceTo(p) > 1 then
        return true
      end
    end
    return false
  end,
  on_use = function(self, event, target, player, data)
    -- 额外结算一次
    data.additionalEffect = (data.additionalEffect or 0) + 1
  end,
})

-- 效果5：造成伤害后目标角色使用的下一张牌无效
Lua__lijiyi:addEffect(fk.Damage, {
    is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(Lua__lijiyi.name) and 
        player:getMark("@@lijiyi_effect5-round") > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    -- 设置目标的"下一张牌无效"标记
    room:setPlayerMark(data.to, "@@lijiyi_disable-round", 1)
  end,
})

Lua__lijiyi:addEffect(fk.CardUsing, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(Lua__lijiyi.name) and target:getMark("@@lijiyi_disable-round") > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    data.toCard = nil
    data:removeAllTargets()
    -- 清除标记并取消使用
    room:removePlayerMark(target, "@@lijiyi_disable-round")
  end,
})

return Lua__lijiyi 